package com.ycsj.common.view.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;

/**
 * 系统弹框的工具类
 */

public class AlertDialogUtil {

    // 取消按钮的文字  确定按钮的文字
    private String mNegBtnText, mPosBtnText;
    //是否可以按返回键关闭
    private boolean cancelable;
    private Context mContext;
    // 取消按钮的点击监听
    private OnAlertNegativeListener mNegativeListener;
    // 确定按钮的点击监听
    private OnAlertPositiveListener mPositiveListener;

    /**
     * 单例模式
     *
     * @param context
     * @return
     */
    public static AlertDialogUtil newInstance(@NonNull Context context) {
        return new AlertDialogUtil(context);
    }

    private AlertDialogUtil(@NonNull Context context) {
        super();
        this.mContext = context;
        cancelable = false;
        mNegBtnText = "取消";
        mPosBtnText = "确定";
    }


    /**
     * 显示对话框
     *
     * @param message 对话框的内容
     */
    public void showDialog(String message) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setMessage(message)
                .setCancelable(cancelable)
                .setPositiveButton(mPosBtnText, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (mPositiveListener != null) {
                            mPositiveListener.onPositiveClick(i);
                        }
                    }
                })
                .setNegativeButton(mNegBtnText, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (mNegativeListener != null) {
                            mNegativeListener.onNegativeClick(i);
                        }
                    }
                }).show();
    }

    /**
     * 是否可以按返回键关闭
     *
     * @param cancelable {@code true}: 可以<br>{@code false}: 不可以
     * @return AlertDialogUtil 类
     */
    public AlertDialogUtil setCancelable(boolean cancelable) {
        this.cancelable = cancelable;
        return this;
    }

    /**
     * 设置取消按钮的文字
     *
     * @param text 取消按钮的文字
     * @return AlertDialogUtil 类
     */
    public AlertDialogUtil negativeText(String text) {
        this.mNegBtnText = text;
        return this;
    }

    /**
     * 设置确定按钮的文字
     *
     * @param text 确定按钮的文字
     * @return AlertDialogUtil 类
     */
    public AlertDialogUtil positiveText(String text) {
        this.mPosBtnText = text;
        return this;
    }

    /**
     * 设置取消按钮的点击事件
     *
     * @param listener 取消按钮的点击事件监听
     * @return AlertDialogUtil 类
     */
    public AlertDialogUtil negative(OnAlertNegativeListener listener) {
        this.mNegativeListener = listener;
        return this;
    }

    /**
     * 设置确定按钮的点击事件
     *
     * @param listener 确定按钮的点击事件监听
     * @return AlertDialogUtil 类
     */
    public AlertDialogUtil positive(OnAlertPositiveListener listener) {
        this.mPositiveListener = listener;
        return this;
    }

    public interface OnAlertNegativeListener {
        void onNegativeClick(int i);
    }

    public interface OnAlertPositiveListener {
        void onPositiveClick(int i);
    }


    /**
     *预习
     *
     */
    public static void showAlertDialogYuxi(boolean isCancel, Context context, String content, final onListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
//        builder.setIcon(R.mipmap.ic_launcher);
        builder.setCancelable(isCancel);
        builder.setTitle("温馨提示");
        builder.setMessage(content);//提示内容
        builder.setPositiveButton("结束预习", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (listener != null) {
                    listener.onClickOk();
                }
            }
        });

        builder.setNegativeButton("再来一次", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (listener != null) {
                    listener.onClickDismiss();
                }
            }
        });

        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(isCancel);
        dialog.show();
    }


    /**
     * 两个回调 接口的方法(确定和取消)
     *
     * @param isCancel 是否允许取消
     * @param context
     * @param content
     * @param listener
     */
    public static void showAlertDialog(boolean isCancel, Context context, String content, final onListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
//        builder.setIcon(R.mipmap.ic_launcher);
        builder.setCancelable(isCancel);
        builder.setTitle("温馨提示");
        builder.setMessage(content);//提示内容
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (listener != null) {
                    listener.onClickOk();
                }
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (listener != null) {
                    listener.onClickDismiss();
                }
            }
        });

        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(isCancel);
        dialog.show();
    }

    /**
     * 两个回调 接口的方法(确定和取消)
     *
     * @param isCancel 是否允许取消
     * @param context
     * @param content
     * @param listener
     */
    public static void showAlertDialog(boolean isCancel, Context context, String content, String text,final onListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
//        builder.setIcon(R.mipmap.ic_launcher);
        builder.setCancelable(isCancel);
        builder.setTitle("温馨提示");
        builder.setMessage(content);//提示内容
        builder.setPositiveButton(text, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (listener != null) {
                    listener.onClickOk();
                }
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (listener != null) {
                    listener.onClickDismiss();
                }
            }
        });

        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(isCancel);
        dialog.show();
    }


    /**
     * 两个回调 接口的方法(确定和取消)
     *
     * @param isCancel 是否允许取消
     * @param context
     * @param content
     * @param listener
     */
    public static void showAlertDialogSimple(boolean isCancel, Context context, String content,final onListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
//        builder.setIcon(R.mipmap.ic_launcher);
        builder.setCancelable(isCancel);
        builder.setTitle("温馨提示");
        builder.setMessage(content);//提示内容
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (listener != null) {
                    listener.onClickOk();
                }
            }
        });

        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(isCancel);
        dialog.show();
    }
    /**
     * 包含点击取消和确定两个
     */

    public interface onListener {
        //点击确定的接口
        void onClickOk();

        //点击取消的接口
        void onClickDismiss();
    }
    /*使用方法:
    *
    *
    *   AlertDialogUtil.newInstance(this)
    *   .setCancelable(false) //点击返回键和空白处是否可以消失
                        .negativeText("")//设置取消按钮的文字,如果设置为空时则不显示取消按钮
                        .negative(new AlertDialogUtil.OnAlertNegativeListener() {
                            @Override
                            public void onNegativeClick(int i) {

                            }
                        })//确定按钮的点击事件
                         .positive(new AlertDialogUtil.OnAlertPositiveListener() {
                            @Override
                            public void onPositiveClick(int i) {

                            }
                        })//取消按钮的点击事件
                        .showDialog("是否显示信息?");
    * */

}
